from django.shortcuts import render_to_response
from garnirechi.admin.forms import ProductForm, WebContentForm, WebContentForm, PhotoalbumForm
from django.http import HttpResponseRedirect
from garnirechi.admin.models import Product, WebContent, Photoalbum
import logging
import gdata.photos.service
import gdata.media
import gdata.geo


def admin_photoalbums(request):
    query = Photoalbum.all()
    albums = query.fetch(10)
    return render_to_response("admin/photoalbums/photoalbums.html", {'albums':albums})

def photoalbum(request, album_id):
    if album_id:
        logging.debug('Album for edit: %s' % (album_id))
        album = Photoalbum.get_by_id(int(album_id))
        form = PhotoalbumForm(instance=album)
        gd_client = gdata.photos.service.PhotosService()

    else:
        logging.debug('Creating form for new product')
        form = PhotoalbumForm()
    return render_to_response("admin/photoalbums/album.html", {"form": form, "album_id":album_id})

def delete_photoalbum(request, album_id):
    if album_id:
        logging.debug('Album for delete: ' + album_id)
        album = Photoalbum.get_by_id(int(album_id))
        Photoalbum.delete(album)
    return HttpResponseRedirect("/admin/photoalbums")

def save_photoalbum(request):
    logging.debug("Saving album")
    if request.method == 'POST':
        album_id = request.POST['album_id']
        logging.debug('album_id: ' + album_id)
        if "" != album_id:
            album = Photoalbum.get_by_id(int(album_id))
            form = PhotoalbumForm(data=request.POST, instance=album)
        else:
            form = PhotoalbumForm(data=request.POST)
        if(form.is_valid()):
            form.save()
            return HttpResponseRedirect("/admin/photoalbums")



def admin_shop(request):
    query = Product.all()
    products = query.fetch(10)
    return render_to_response("admin/shop/shop.html", {"products":products})


def product(request, product_id):
    if product_id:
        logging.debug('Product for edit: %s' % (product_id))
        product = Product.get_by_id(int(product_id))
        form = ProductForm(instance=product)
    else:
        logging.debug('Creating form for new product')
        form = ProductForm()
    return render_to_response("admin/shop/product.html", {"form": form, "product_id":product_id})

def delete_product(request, product_id):
    if product_id:
        logging.debug('Product for delete: ' + product_id)
        product = Product.get_by_id(int(product_id))
        Product.delete(product)
    return HttpResponseRedirect("/admin/shop")

def save_product(request):
    logging.debug("Saving product")
    if request.method == 'POST':
        product_id = request.POST['product_id']
        logging.debug('product_id: ' + product_id)
        if "" != product_id:
            product = Product.get_by_id(int(product_id))
            form = ProductForm(data=request.POST, instance=product)
        else:
            form = ProductForm(data=request.POST)
        if(form.is_valid()):
            form.save()
            return HttpResponseRedirect("/admin/shop")

def admin_webcontent(request):
    query = WebContent.all()
    contents = query.fetch(10)
    return render_to_response("admin/webcontent/webcontent.html", {"contents":contents})


def webcontent(request, content_id):
    if content_id:
        logging.debug('Content for edit: ' + content_id)
        content = WebContent.get_by_id(int(content_id))
        form = WebContentForm(instance=content)
    else:
        logging.debug('Creating form for new content')
        form = WebContentForm()
    return render_to_response("admin/webcontent/content.html", {"form": form, "content_id":content_id})

def delete_webcontent(request, content_id):
    if content_id:
        logging.debug('Content for delete: ' + content_id)
        webcontent = WebContent.get_by_id(int(content_id))
        WebContent.delete(webcontent)
    return HttpResponseRedirect("/admin/webcontent")

def save_webcontent(request):
    logging.debug("Saving webcontent")
    if request.method == 'POST':
        content_id = request.POST['content_id']
        logging.debug('content_id: ' + content_id)
        if "" != content_id:
            webcontent = WebContent.get_by_id(int(content_id))
            form = WebContentForm(data=request.POST, instance=webcontent)
        else:
            form = WebContentForm(data=request.POST)
        if(form.is_valid()):
            form.save()
            return HttpResponseRedirect("/admin/webcontent")

